<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<%@ include file="/common-app/taglibs.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title>在填申请项目查询</title>
<script type="text/javascript" src="${res}/js/proposal/irisPrpPrepare.js"></script>
<script type="text/javascript" src="${res_app}/js/app/proposal/irisPrpPrepare.js"></script>
<script type="text/javascript" src="${res}/js/jquery/jquery.json.js"></script>
</head>

<body>
	<input type="hidden" id="childDiv" class="thickbox" />
	<form method="post" id="mainForm">
			<input type="hidden" id="prpCodes" name="prpCodes" value="" />
			<input type="hidden" id="type" name="type" value="" />
		</form>

	<div class="soBox">
		<div class="detail ico_search">
			<table>
				<tr>
					<td rowspan="2"><strong>检索条件：</strong>
					</td>
					<td>
						<cpt:search key="prpApprove_search_dept"  repeatColumn="3" displayCount="5" leftStyle="width:100px"></cpt:search>
					</td>
					
				</tr>
				<tr>
				<td>
					<div class="clear_h10"></div>
					<input type='button' id='searchBt' class="button_an" value='查询' />
				</td>
				</tr>
			</table>
		</div>
	</div>
	<div class="clear_h10"></div>
	<div>
		<div id='TopBarMnt'></div>
		<table id='dataGrid'></table>
		<div id='dataBar'></div>
	</div>
	
				<br/>
				<p>将所选项目的所属部门设置为：<cpt:select id="depts" key="deptList" onChange="toSelectValuesByDeptCode(this)"></cpt:select></p>
	<script type="text/javascript">
	var irisGrid = new IrisGrid("dataGrid", "dataBar", 'TopBarMnt');
	var prcWin = new progressWin("${res}");//初始化遮罩层
	function showApproveDialog(type){
		var prpCodes="";
		
		$(":checkbox:checked").each(function(){
			var rowId=$(this).attr("id");
			var id=rowId.substring(rowId.lastIndexOf("_")+1,rowId.length);//取出grid中需要的id
			if(id!=null&&id!=""&&$.isNumeric(id))
				prpCodes=prpCodes+id+",";
		});
		
		if(prpCodes==""){
			scmError("请选择一条记录");
			return false;
		}
		if(type=="approve"){
				
				var prpnum = prpCodes.split(",").length-1;
				$("#prpCodes").val(prpCodes);
				$("#type").val(type);
				var options = {
						beforeSubmit : function() {
							prcWin.openWin();//打开遮罩层
							$("#approve").attr("disabled",true);
						}, // 表单提交前的回调函数
						success : function(msg) {
							prcWin.closeWin();
							scmSuccess(msg);
							irisGrid.getGridDom().jqGrid("refresh",0);//0是本页1是首页
							$("#approve").attr("disabled",false);
						},// 表单成功提交后的回调函数
						error : function() {
							scmError("操作失败,请联系系统管理员");
							prcWin.closeWin();
							$("#approve").attr("disabled",false);
						},
						url: "${ctx}/prpapprove/"+type
					};
				if(confirm("是否确认此"+prpnum+"份申请书?")){
					$("#mainForm").ajaxSubmit(options);
				}
				else{
					return;
				}
			
		}else{
			var url="${ctx}/prpapprove/openprpapprovedialog?prpCodes="+prpCodes+"&type="+type+"&TB_iframe=true&height=220&width=400";
			var title="申报审核意见（最多为500字）";
			showThickBox(url,title);
		}
		
	}
	
	function checkSelect(){
		var selectArray=irisGrid.getGridDom().jqGrid("getGridParam", 'selarrrow');//获得选中行
		$("#approve").removeAttr("disabled");//去除该属性
		for(var i=0;i<selectArray.length;i++){
			var status=irisGrid.getGridDom().jqGrid('getCell',selectArray[i],"13");
			var flagCan=irisGrid.getGridDom().jqGrid('getCell',selectArray[i],"14");
			if(status=='01'&&flagCan=="0") {
				$("#approve").attr("disabled","disabled");
				scmError("您所选择的这"+selectArray.length+"份申请书中有部分申请书的正式PDF未生成，请在生成之后再选择该申请书进行确认操作");
			}
		}
	}
	
	function viewComment(des3Prpcode){
		var url="${ctx}/proposal/view/audit-comment-view?posCode="+des3Prpcode+"&TB_iframe=true&height=390&width=720";
		var title="审核意见";
		showThickBox(url,title);	
	}
	
	function closeDialog(){
		tb_remove();
	}

	
		/** 表格初始化**/
		//loadIrisGrid("${res}");
		function buildGrid() {
			var columnAttrList = new Array();
			columnAttr = new ColumnAttr("项目类别", "grant_name","grant_name", 4, "left", true);
			columnAttrList.push(columnAttr);
			
			columnAttr = new ColumnAttr("项目名称", "zh_title","zh_title", 6, "left", true);//表头，字段名，，，宽度比例，居左，是否需要表头排序
			columnAttrList.push(columnAttr);
			
			columnAttr = new ColumnAttr("负责人", "psn_name","psn_name", 4, "left", true);
			columnAttrList.push(columnAttr);
			
			columnAttr = new ColumnAttr("提交时间", "submitDate", "submit_date", 3,"left", true);
			columnAttrList.push(columnAttr);
			
			columnAttr = new ColumnAttr("水印号", "prp_version","prp_version", 3, "left", true);
			columnAttrList.push(columnAttr);
			
			columnAttr = new ColumnAttr("受理编号", "prp_no","prp_no", 3, "left", true);
			columnAttrList.push(columnAttr);
			
			columnAttr = new ColumnAttr("工作进度", "statusName", "status", 4,"center", true);
			columnAttrList.push(columnAttr);
			columnAttr = new ColumnAttr("所属二级部门", "dept_name", "dept_name", 4,"center", true);
			columnAttrList.push(columnAttr);
			columnAttr = new ColumnAttr("审核意见", "comment", "comment", 2,"center", false);
			columnAttrList.push(columnAttr);
			
			columnAttr = new ColumnAttr("PDF", "pdf", "pdf", 2,"center", false);
			columnAttrList.push(columnAttr);
			
	        columnAttr = new ColumnAttr("status", "status", "status", 3,"left", false,false,"text",true);
			columnAttrList.push(columnAttr);
			columnAttr = new ColumnAttr("flagCan", "flagCan", "flagCan", 3,"left", false,false,"text",true);
			columnAttrList.push(columnAttr);
			columnAttr = new ColumnAttr("grantCode", "grantCode", "grantCode", 3,"left", false,false,"text",true);
			columnAttrList.push(columnAttr);
			irisGrid.setColumnAttrs(columnAttrList);
			irisGrid.setToolsBar([{
					id : 'saveDeptListMsg',
					name : '保存部门设置',
					trigger : saveDeptListMsg
				}]);
		 	
		 	irisGrid.setUrl("${ctx}/prpapprove/list-for-manage");//查询url
			irisGrid.setCaption("暂写");
			irisGrid.setDefaultSortName("status");//默认排序字段
			irisGrid.setDefaultSortorder("asc");
			irisGrid.setWidth(978);//整个表格宽度
			irisGrid.setCheckAble(true);//表格左边是否要复选框
			irisGrid.setShowRowIndex(true);//是否显示行号
			irisGrid.setQueryBox('searchBt', '<c:if test="${amount==0 }">prpApprove_search_dept</c:if><c:if test="${amount!=0 }">prpApprove_search2_rc</c:if>');//查询条件 按钮名，查询条件function_key
			irisGrid.onSelectAll = function(selectid,isSelect){//全选事件
				//checkSelect()
			}
			irisGrid.onSelectRow = function(selectid,isSelect){//选择事件	
				//checkSelect();
			}
			irisGrid.build();
		}
		jQuery(function() {
			
			buildGrid();
		});
		
		
		function toSelectValuesByDeptCode(obj) {
			
            var posCodes = getSelOpt();
           if(posCodes==""){
        	   $("#depts").val("");
        	   alert("请至少选择一个项目再操作!");
           }
			
		    if(posCodes!=""){
		    	var posCode = posCodes.split(",");
		    	 for(var i=0;i<posCode.length;i++){
		    	$("#dept_"+posCode[i]).val(obj.value);
		    		
	        	} 
				
			}
       
		}
		
		//获取选择的项目
		var getSelOpt = function() {
			var g = "";
			$("input:checkbox[id^=jqg_dataGrid_]").each(function(i, obj) {
				if (obj.checked) {
					g += "," + obj.id.replace("jqg_dataGrid_", "");
				}
			});
			if (g != "") {
				g = g.substring(1);
			}
			return $.trim(g);
		};
		
		
		//获取所有的项目
		var getSelOpt2 = function() {
			var g = "";
			$("input:checkbox[id^=jqg_dataGrid_]").each(function(i, obj) {
				g += "," + obj.id.replace("jqg_dataGrid_", "");
				
			});
			if (g != "") {
				g = g.substring(1);
			}
			return $.trim(g);
		};
		
		//根据psnCodes查询某一行中需要更新的值拼成json的形式
		function getXMLDataItems(posCodes) {

			var arr = new Array();
			var posCode = posCodes.split(",");
			for ( var i = 0; i < posCode.length; i++) {
				var obj = new Object();
				var dept_code="";
				if($("#depts").val()=="") dept_code = $("#dept_" + posCode[i]).val();
				if($("#depts").val()!="") dept_code = $("#depts").val();
				
				var old_dept_code = $("#old_dept_" + posCode[i]).val();
				
				if(dept_code==old_dept_code){
                	continue;
                }
              
				obj.pos_code = posCode[i];
				obj.dept_code = dept_code;
				arr.push(obj);
			}
         
			return $.toJSON(arr);
		}
		
		var  saveDeptListMsg= function(gridObj, selIdstr, recordCount) {
			
			var posCodes = getSelOpt();
			var depts = $("#depts").val();
			if(posCodes == ""&&depts!=""){
				alert("请至少选择1条记录再操作！");
				return ;
			}
			if(posCodes != ""&&depts==""){
				alert("请选择所选项目的所属部门设置选项");
				return ;
			}
			
		    if(posCodes=="") posCodes= getSelOpt2();
			
			if(posCodes==""){
				alert("无任何记录需要更新数据，您不需要保存!");
		    	return ;
			}
			var	parameters = getXMLDataItems(posCodes);
			
			
		   if (parameters == "[]"){
		    	alert("列表中的记录没有发生任何的修改，您不需要保存!");
		    	return ;
		    }
			
		   if (confirm("您确定是否保存所选中或列表中的项目？")) {
					var options = {
						type : 'post',
						data : {
							'parameters' : parameters
						},
						beforeSend : function() {
						}, // 表单提交前的回调函数
						success : function(msg) {
							if (msg == 'SQLError') {
								prcWin.closeWin();// 关闭遮罩层
								scmError("操作失败！");
							} else {
								prcWin.closeWin();// 关闭遮罩层
								scmSuccess("操作成功！");
								irisGrid.getGridDom().jqGrid("refresh", 0);//0是本页1是首页

							}
						},// 表单成功提交后的回调函数
						error : function() {
							scmError("操作失败！");
							prcWin.closeWin();
						},
						url : "${ctx}/prpapprove/saveDeptListMsg"
					};

					$.ajax(options);
				}
		};
		
		
		/**查看单位信息*/
		function orgInfoView(orgCode){
			//$("#orgCode").val(orgCode);
			//$("#mainForm").submit();
			tb_show("查看单位信息","${ctx}/orgmanage/verifyorg/org-info-view?orgCode=" + orgCode
					+ "&TB_iframe=true&height=460&width=950", false);
		}
	</script>
</body>
</html>